import { Body, Controller, Get, Inject, Post, Query } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import e from "express";
import { PageResult } from "src/common/page/PageResult";
import { Result } from "src/common/Result";
import { Unit } from "src/entity/Unit";
import { UnitService } from "src/service/UnitService";

@ApiTags("单位模块")
@Controller("/unit")
export class UnitController {

    @Inject()
    private unitService: UnitService;

    /**
     * 单位列表查询
     */
    @ApiOperation({summary: "单位列表查询"})
    @Get("/selectUnitList")
    async selectList(@Query() unit: Unit) {
        let data;
        try {
            data = await this.unitService.selectList(unit);
        } catch (error) {
            return Result.failed(error);
        }
        return Result.success("单位列表查询", PageResult.of(data));
    }

    /**
     * 添加商品
     * @param unit 
     */
    @ApiOperation({summary: "添加商品"})
    @Post("/add")
    async add (@Body() unit: Unit) {
        return this.unitService.add(unit);
    }

    /**
     * 单位编辑
     * @param unit 
     */
    @ApiOperation({summary: "单位编辑"})
    @Post("/update")
    async update(@Body() unit: Unit) {
        return this.unitService.update(unit);
    }
}